import { onActivated, onMounted } from 'vue'
import { useRoute } from 'vue-router'

export default function useRouteParam(key: string, onChange: (value: string) => void) {
  let param: string | null = null
  const route = useRoute()

  onActivated(() => {
    if (param === route.params[key]) return
    param = route.params[key] as string
    onChange(param)
  })
  onMounted(() => (param = route.params[key] as string))
}
